package com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
import com.dto.User;
import com.service.AsyncServer;
import com.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


/**
 * @author HuangSuip
 */
@RequestMapping("/user")
@Controller
//@Transactional
//@Scope("request")
public class UserController {

    private final Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private UserService userService;

    @Autowired
    private AsyncServer asyncServer;

    @Autowired
    private User user;

    @GetMapping(value = "/name")
    @ResponseBody
    public String getUserName() {
        System.out.println(this);
        System.out.println(this.userService);
        return userService.toString() + this.toString() + String.valueOf(user);
    }

    //访问template目录
    @GetMapping(value = "/modelandview")
    public ModelAndView getModelAndView(Model model) {
        return new ModelAndView("user");
    }

    @GetMapping(value = "/view")
    public String getView(Model model) {
        return "user";
    }

    @GetMapping(value = "/view1")
    public String getView1(Model model) {
        return "user1";
    }

    @GetMapping(value = "/view2")
    public String getView2(Model model) {
        return "user2";
    }

    @GetMapping(value = "user", params = "user=admin")
    @ResponseBody
    public String getUser() throws InterruptedException, ExecutionException {
        Future<String> future = asyncServer.doTask1();
        if (future != null) {
            logger.info(future.toString());
        }
        logger.info(Thread.currentThread().getName());
        return asyncServer.doTask2().get();
    }

    @GetMapping("date")
    @ResponseBody
    public Map getDate() {
        Map<String, Object> map = new HashMap<>();
        map.put("date", new Date());
        return map;
    }


    @PostMapping("/returnUser")
    @ResponseBody
    private User returnUser(
        @RequestBody(required = false) User user,
        @RequestParam int age,
        @RequestParam String name
    ) {
        User user1 = new User();

        return user1;
    }

    @GetMapping("/newUser")
    @ResponseBody
    public User user(
        int id, int age, String name
    ) {
        User user = new User();
        user.setName("已知x1、x2是关于x的方程\uD83D\uDCF7的两个实根，且\uD83D\uDCF7，求m的值");
        user.setName(name);
        user.setId((long) id);
        user.setAge(age);
        System.out.println(this);
        System.out.println(this.userService);
        userService.insert(user);

        return user;
    }

    @PostMapping("form-data")
    @ResponseBody
    public String fromData(HttpServletRequest request) {

        return request.getParameter("user") + "  " + request.getParameter("name");
    }

    @ResponseBody
    @RequestMapping(method = {RequestMethod.GET}, value = "/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}.{extension:\\.[a-z]}")
    public String fromData2(
        HttpServletRequest request,
        @PathVariable(name = "symbolicName") String symbolicName,
        @PathVariable(name = "version") String version,
        @PathVariable(name = "extension") String extension
    ) {
        System.out.println(symbolicName);
        return request.getParameter("user") + "  " + request.getParameter("name");
    }
}
